package com.by.proxy;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/8 20:24
 * @注释
 */
import com.by.aop.MethodInterceptor;
import com.by.aop.MethodInvocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
    private Object target;
    private MethodInterceptor interceptor;

    public ProxyFactory(Object target, MethodInterceptor interceptor) {
        this.target = target;
        this.interceptor = interceptor;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        MethodInvocation invocation = new MethodInvocation(target, method, args);
                        return interceptor.invoke(invocation);
                    }
                });
    }
}